<template>
	<div class="store-room">
    <div class="table-title">
      <el-button type="primary" @click="handleAdd" data-auth="store_travel_agency_add">添加旅行社</el-button>
    </div>
    <div class="table-content">
      <el-table
        border
        :data="tableData"
        style="width: 100%">
				<el-table-column
					label="名称"
					prop="Name">
				</el-table-column>
				<el-table-column
					label="类型"
					prop="AgencyType">
          <template slot-scope="scope">
            {{scope.row.AgencyType===1?'旅行社':'OTA'}}
          </template>
				</el-table-column>
        <el-table-column label="操作" width="250px">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)" data-auth="store_travel_agency_modify">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)" data-auth="store_travel_agency_delete">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="table-page">

    </div>

    <el-dialog
      :title="dialogTitle"
      :visible.sync="roomColShow"
      width="550px"
      center>
      <el-form :model="colForm" :rules="policyRules" ref="colForm" label-width="140px">
        <el-form-item label="名称：" prop="Name">
          <el-input v-model="colForm.Name"></el-input>
        </el-form-item>
        <el-form-item label="类型：" prop="AgencyType">
          <el-select v-model="colForm.AgencyType" placeholder="请选择">
            <el-option :label="'旅行社'" :value="1"></el-option>
            <el-option :label="'OTA'" :value="2"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="policyAddEdit('colForm')">{{dialogTitle==='添加'?'确认添加':'确认修改'}}</el-button>
        <el-button @click="roomColShow = false">取 消</el-button>
      </span>
    </el-dialog>

	</div>
</template>
<script>
	  export default {
        data () {
						let _this=this;
            return {
              dialogTitle:'',
              roomColShow:false,
              tableData: [],
							PolicyConfigList:[],
              colForm: {
                Name: '',
                AgencyType:1
              },
							policyRules: {
								Name: [
									{ required: true, message: '请输入名称', trigger: 'blur' }
								],
                AgencyType: [
									{ required: true, type:'number',message: '请选择类型', trigger: 'change' }
								]
							}
            }
        },
        created(){
					this.getList();
        },
        methods:{
					initList(){
						this.roomColShow=false;
						this.getList();
					},
					getList(){
						this.$ajax.GetConfigTypeList({hotelId:JSON.parse(sessionStorage.getItem('nowmenu')).Id}).then(res=>{
							if(res.Success&&res.Data){
								this.tableData=res.Data;
							}
						})
					},
          resetForm(formName) {
            if(!this.$refs[formName]){return};
            this.$refs[formName].resetFields();
          },
          handleAdd(index, row) {
            this.dialogTitle='添加';
            this.resetForm('colForm');
						this.colForm={
							Name: ''
						},
            this.roomColShow=true;
          },
          handleEdit(index, row) {
            this.dialogTitle='编辑';
						this.resetForm('colForm');
						this.colForm=_.cloneDeep(row);
            this.roomColShow=true;
          },
          handleDelete(index, row) {
            this.deleteShow('确认删除?',row,'DeleteTravelAgency')
          },
          deleteShow(title,id,api){
            this.$confirm(title, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning',
              center: true
            }).then(() => {
							let data={
								id:api==='DeleteTravelAgency'?id.Id:id
							};
							this.$ajax[api](data).then(res=>{
								if(res.Success){
									this.$message({
		                type: 'success',
		                message: '已删除!'
		              });
									this.initList();
								}
							})
            }).catch(() => {
            });
          },
					policyAddEdit(formName){
						this.$refs[formName].validate((valid) => {
              if (valid) {
								let data=_.cloneDeep(this.colForm);
                this.$ajax.SaveTraveAgency(data).then(res=>{
									if(res.Success){
										this.$message.success(this.dialogTitle+'成功!');
										this.$refs[formName].resetFields();
										this.initList();
									}
                })
              } else {
                return false;
              }
            });
					},
        }
	  }
</script>
